<template>
<div :style="{height:height,width:width}" class="ctn">
    <div id='train-map' :class="className" :style="{height:height,width:width}"/>
    <div class="stations">
        <div v-for="(stations, i) in cutTrainLine" :key='i' class="col">
            <div v-for="d in stations" :key="d.code" class="station">
                <div class="name">{{d.name}}</div>
                <div class="code">{{d.code}}</div>
            </div>
        </div>
    </div>
</div>
    
</template>

<script>
import echarts from "echarts";
// require('echarts/map/js/china-contour.js') 
require('echarts/map/js/china.js') 
import resize from "./mixins/resize";
import _ from 'lodash';

import provinces from './mock/provinces'
import trainLine from './mock/trainLine'

const geoTraiLine = _.filter(trainLine, d=>d.coord)

const animationDuration = 1000;

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: "chart"
        },
        width: {
            type: String,
            default: "100%"
        },
        height: {
            type: String,
            default: "100%"
        }
    },
    data() {
        return {
            chart: null,
            trainLine
        };
    },
    computed: {
        cutTrainLine(){
            let size = Math.ceil(this.trainLine.length / 4);
            let cols = _.chunk(this.trainLine, size);
            return cols;
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart();
        });
    },
    beforeDestroy() {
        if (!this.chart) {
            return;
        }
        this.chart.dispose();
        this.chart = null;
    },
    methods: {
        initChart() {
            this.drawChart();
        },
        drawChart() {
            let scatter = {
                type: 'scatter',
                coordinateSystem: 'geo',
                symbolSize: 5,
                itemStyle: {
                    borderWidth: 2,
                    borderColor: '#58a5af'
                }
            };
            let scatterData = _.map(geoTraiLine, d=>{
                return {
                    name: `${d.name} (${d.code})`,
                    value: d.coord.concat(1)    
                };
            });
            this.chart = echarts.init(document.getElementById('train-map'), 'macarons')
            //禁用地图高亮
            this.chart.on("mouseover", (params)=>{
                if(params.seriesType == "map"){
                    this.chart.dispatchAction({
                        type: 'downplay'
                    });
                }
            });
            this.chart.setOption({
                visualMap: {
                    left: 'right',
                    min: 0,
                    max: 100,
                    inRange: {
                        color: ['#042544', '#235787']
                    },
                    text: ['High', 'Low'],           // 文本，默认为数值文本
                    calculable: true,
                    show: false
                },
                geo: {
                    map: 'china',
                    center: [95.66031072,33.01034566],
                    zoom: 6,
                    roam: true,
                    emphasis: {
                        label: {
                            color: '#fffe',
                            show: false
                        },
                        itemStyle: {
                            areaColor: '#2e82cb',
                            opacity: 0.2
                        },
                    },
                },
                series: [{
                    type: 'map',
                    geoIndex: 0,
                    data: provinces
                }, {
                    type: 'lines',
                    coordinateSystem: 'geo',
                    polyline: true,
                    data: [{
                        coords: _.map(geoTraiLine, 'coord'),
                        lineStyle: {
                            width: 3,
                            color: '#58a5af'
                        }
                    }]
                }, {
                    ...scatter,
                    data: _.filter(scatterData, (d, i)=>i%2 == 0),
                    label: {
                        show: true,
                        position: 'right',
                        formatter: '{b}',
                        distance: 10,
                        color: '#fffd',
                        fontSize: 10
                    },
                }, {
                    ...scatter,
                    label: {
                        show: true,
                        position: 'left',
                        formatter: '{b}',
                        distance: 10,
                        color: '#fffd',
                        fontSize: 10
                    },
                    data: _.filter(scatterData, (d, i)=>i%2 == 1)
                }]
            });
        }
    }
};
</script>

<style lang="scss" scoped>
.ctn {
    position: relative;
    .stations {
        position: absolute;
        right: 0;
        bottom: 0;
        color: #0ebfbf;
        font-size: 10px;
        display: flex;
        border: 1px solid #0ebfbf;
        background-color: #0ebfbf22;
        box-shadow: 0 0 10px 0 #000;
        .col {
            border-right: 1px solid rgb(2, 56, 56);
            padding: 5px 0;
            .station {
                display: flex;
                justify-content: space-between;
                padding: 1px 10px;
                &:hover{
                    background-color: #fff2;
                }
                .name{
                    margin-right: 5px;
                }
                & > *{
                    cursor: default;
                }
            }
        }
    }
}
</style>
